package com.peng.wx_official_account.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.peng.wx_official_account.entity.enums.AreaEnum;
import com.peng.wx_official_account.service.CopywritingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
@RequiredArgsConstructor
@SuppressWarnings("DuplicatedCode")
public class WeChatPushDataService {

    private final WeatherService weatherService;
    private final CopywritingService copywritingService;

    /**
     * Templates of wxy map.
     * WXY专用模板
     *
     * @return the map
     */
    public Map<String, Map<String, String>> templatesOfWXY() {
        Map<String, Map<String, String>> data = new HashMap<>();

        //获得天气信息
        int tempNum;
        String weather;
        Map<String, String> weatherMap;

        try {
            weatherMap = weatherService.getWeather(AreaEnum.HANG_ZHOU.getLocation());
            tempNum = Integer.parseInt(weatherMap.get("temperature"));
            weather = weatherMap.get("weather");
        } catch (Exception e) {
            log.error("WXY推送。。。获取天气信息失败");
            throw new RuntimeException(e);
        }

        HashMap<String, String> dataMap = new HashMap<>();
        dataMap.put("dateTime", DateUtil.format(LocalDateTime.now(), "yyyy年MM月dd日"));
        dataMap.put("address", "杭州市");
        dataMap.put("titleOne", "天气");
        dataMap.put("weather", weather + "天");
        dataMap.put("titleTwo", "温度");
        dataMap.put("temperature", tempNum + "°C");
        dataMap.put("titleTree", "低");
        dataMap.put("low", (tempNum - 5) + "°C");
        dataMap.put("titleFour", "高");
        dataMap.put("height", (tempNum + 5) + "°C");
        dataMap.put("titleFive", "相识");
        dataMap.put("acquaintance", String.valueOf(DateUtil.between(DateUtil.parseDate("2022-02-12"), DateUtil.date(), DateUnit.DAY)));
        dataMap.put("titleSix", "小笨蛋");
        dataMap.put("titleSeven", "生日");
        //获得生日倒计时
        long birthdayCountDown = parseBirthday("12-06");
        dataMap.put("days", String.valueOf(birthdayCountDown));

        String remark = birthdayCountDown == 0 ? "Happy Birthday,祝你越来越美,开心快乐每一天!" : copywritingService.getOneOfCopywritingList();
        dataMap.put("remark", StringUtils.isNoneBlank(remark) ? remark : "陪伴是最长情的告白！");

        for (Map.Entry<String, String> entry : dataMap.entrySet()) {
            HashMap<String, String> map = new HashMap<>();
            map.put("value", entry.getValue());
            map.put("color", "#3498db");
            data.put(entry.getKey(), map);
        }
        return data;
    }

    public Map<String, Map<String, String>> templatesOfWXYPlus() {
        Map<String, Map<String, String>> data = new HashMap<>();

        //获得天气信息
        int tempNum;
        String weather;
        Map<String, String> weatherMap;

        try {
            weatherMap = weatherService.getWeather(AreaEnum.HANG_ZHOU.getLocation());
            tempNum = Integer.parseInt(weatherMap.get("temperature"));
            weather = weatherMap.get("weather");
        } catch (Exception e) {
            log.error("WXY推送。。。获取天气信息失败");
            throw new RuntimeException(e);
        }

        HashMap<String, String> dataMap = new HashMap<>();
        dataMap.put("dateTime", DateUtil.format(LocalDateTime.now(), "yyyy年MM月dd日"));
        dataMap.put("address", "杭州市");
        dataMap.put("weather", weather + "天");
        dataMap.put("temperature", tempNum + "°C");
        dataMap.put("acquaintance", String.valueOf(DateUtil.between(DateUtil.parseDate("2022-02-12"), DateUtil.date(), DateUnit.DAY)));
        dataMap.put("together", String.valueOf(DateUtil.between(DateUtil.parseDate("2023-02-14"), DateUtil.date(), DateUnit.DAY)));
        //获得生日倒计时
        long birthdayCountDown = parseBirthday("12-06");
        dataMap.put("days", String.valueOf(birthdayCountDown));

        String remark = birthdayCountDown == 0 ? "Happy Birthday,祝你越来越美,开心快乐每一天!" : copywritingService.getOneOfCopywritingList();
        dataMap.put("remark", StringUtils.isNoneBlank(remark) ? remark : "陪伴是最长情的告白！");

        for (Map.Entry<String, String> entry : dataMap.entrySet()) {
            HashMap<String, String> map = new HashMap<>();
            map.put("value", entry.getValue());
            map.put("color", "#3498db");
            data.put(entry.getKey(), map);
        }
        return data;
    }

    /**
     * Templates of ljp map.
     * LJP专用模板
     *
     * @return the map
     */
    public Map<String, Map<String, String>> templatesOfLJPAndCKT(Integer type) {
        Map<String, Map<String, String>> data = new HashMap<>();

        //获得天气信息
        Map<String, String> weatherMap;
        if (type == 0) {
            weatherMap = weatherService.getWeather(AreaEnum.HANG_ZHOU.getLocation());
        } else {
            weatherMap = weatherService.getWeather(AreaEnum.SHANG_QIU.getLocation());
        }

        Integer tempNum = Integer.valueOf(weatherMap.get("temperature"));

        HashMap<String, String> dataMap = new HashMap<>();
        dataMap.put("date", DateUtil.format(LocalDateTime.now(), "yyyy年MM月dd日"));
        dataMap.put("address", type == 0 ? "杭州市" : "商丘市");
        dataMap.put("weather", weatherMap.get("weather") + "天");
        dataMap.put("temperature", tempNum + "°C");
        long birthdayOfLjp = parseBirthday("10-28");
        long birthdayOfCkt = parseBirthday("10-06");
        dataMap.put("days", type == 0 ? String.valueOf(birthdayOfLjp) : String.valueOf(birthdayOfCkt));
        dataMap.put("years", String.valueOf(DateUtil.between(DateUtil.parseDate(DateUtil.date().year() + "-12-31"), DateUtil.date(), DateUnit.DAY) + 2));

        String remark = copywritingService.getOneOfCopywritingList();
        dataMap.put("remark", StringUtils.isNoneBlank(remark) ? remark : "成为强者的道路总是孤独的！");

        for (Map.Entry<String, String> entry : dataMap.entrySet()) {
            HashMap<String, String> map = new HashMap<>();
            map.put("value", entry.getValue());
            map.put("color", "#55efc4");
            data.put(entry.getKey(), map);
        }
        return data;
    }

    /**
     * Test templates.
     * 测试模板数据填充
     */
    public Map<String, Map<String, String>> testTemplates(String str) {
        Map<String, Map<String, String>> data = new HashMap<>();

        HashMap<String, String> dataMap = new HashMap<>();

        String remark = copywritingService.getOneOfCopywritingList();
        dataMap.put("value", StringUtils.isNoneBlank(str) ? str : remark);
        dataMap.put("color", "#000b62");
        data.put("msg", dataMap);

        return data;
    }

    /**
     * Parse birthday string.
     * 计算生日的时间
     *
     * @param birthdayDateStr the birthday date str
     * @return the long
     */
    public long parseBirthday(String birthdayDateStr) {
        //判断今年是否过了生日
        DateTime birthday = DateUtil.parse(DateUtil.date().year() + "-" + birthdayDateStr);
        //获得生日倒计时
        return DateUtil.between(DateUtil.parseDate((DateUtil.date().isAfter(birthday) ? DateUtil.date().year() + 1 : DateUtil.date().year()) + "-" + birthdayDateStr), DateUtil.date(), DateUnit.DAY) + 1;
    }


}
